//
//  HotViewcontroller.m
//  YSBEmployer
//
//  Created by developer on 2021/12/05.
//  Copyright © 2021 ysb. All rights reserved.
//

#import "YSBOrderDetailNewViewController.h"
#import "YSBOrderDetailPushAnalysisTableViewCell.h"

#import "YSBOfflineOrderInfoAnalysisCell.h"
#import "YSBOfflineOrderInfoViewController.h"


@implementation YSBOrderDetailPushAnalysisTableViewCell

- (void)setModel:(YSBOfflineAnalysisModel *)model {
    _model = model;
     
    self.pushNumLabel.text = [NSString stringWithFormat:@"%ld",model.push_nums];
    self.abandonLabel.text = [NSString stringWithFormat:@"%ld",model.abandon_nums];
    self.grabbedLabel.text = [NSString stringWithFormat:@"%ld",model.take_nums];
    
    
    YSBOfflineReasonModel *reason = model.reason;
    NSArray *reasonArray = @[@"工价太低",@"标题不清晰",@"考虑中",@"放弃接单"];
    for (NSInteger i = 0; i < reasonArray.count; i++) {
        NSString *numStr = @"";

        switch (i) {
            case 0:{
                if (reason.less_money >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.less_money];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.less_money];
                }
                self.priceReasonLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            case 1:{
                if (reason.unclear >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.unclear];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.unclear];
                }
                self.titleReasonLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            case 2:{
                if (reason.collect >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.collect];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.collect];
                }
                self.consideringLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            case 3:{
                if (reason.abandon >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.abandon];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.abandon];
                }
                self.giveUpLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            default:
                break;
        }
    }
    
    [self.contentView removeAllSubviews];
    self.contentView.hidden = true;
    
}


@end


@implementation YSBOrderDetailNewViewController


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cancel_type = self.orderDetailModel.infoModel.cancel_type; //1-超时未支付，2未支付取消，3进行中取消
    if (indexPath.section == 0) {
        if (self.orderStatus == -1 && cancel_type.integerValue != 3) { //临时工 取消订单
            return 180;
        }
        return 250;
        
    }else if (indexPath.section == 1) {
        
        return 0.1;
        
    }else if (indexPath.section == 2) {

//        return UITableViewAutomaticDimension;
        
        NSMutableString *time = [NSMutableString string];
        
        if (![YSBCommonTool isBlankString:self.orderDetailModel.infoModel.work_time_str]) {
            [time appendFormat:@"%@",self.orderDetailModel.infoModel.work_time_str];
        }
        
        CGSize timecellSize = [NSString sizeWithText:time maxContentSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width-10*2-15*2-62, CGFLOAT_MAX) font:[UIFont systemFontOfSize:13] context:nil lineSpaceing:15];
        return 190 - 20 + timecellSize.height;

    }else {
        
        if (indexPath.section == 3 && self.orderDetailModel.infoModel.insurance) {
            if (self.insuranceCell.insuranceBtn.isSelected) {
                return 120;
            }
            return 36;
        }else if ((indexPath.section == 3 && !self.orderDetailModel.infoModel.insurance)
                 || (indexPath.section == 4 && self.orderDetailModel.infoModel.insurance)) {
            
            return [YSBOrderDetailPositionDescribeTableViewCell heightForCellWithOrderDetailAndInfo:self.orderDetailModel.infoModel.contents];
            
        }else if ((indexPath.section == 4 && !self.orderDetailModel.infoModel.insurance)
                     || (indexPath.section == 5 && self.orderDetailModel.infoModel.insurance)) {
                
            return [YSBOrderDetailPositionDescribeTableViewCell heightForCellWithOrderDetailAndInfo:self.orderDetailModel.infoModel.other_contents];
                
        }else if ((indexPath.section == 5 && !self.orderDetailModel.infoModel.insurance)
                  || (indexPath.section == 6 && self.orderDetailModel.infoModel.insurance)) {
 
//            return UITableViewAutomaticDimension;
            
            //定位地址
            CGFloat addressWidth = [[UIScreen mainScreen] bounds].size.width - 10*2 - 15*2 - 62 -20;
            NSString *firstAddressStr = [NSString stringWithFormat:@"%@",[YSBCommonTool isBlankString:address.location_address] ?  @"" : address.location_address];
            CGSize firstAddressSize = [NSString sizeWithText:firstAddressStr maxContentSize:CGSizeMake(addressWidth, CGFLOAT_MAX) font:[UIFont systemFontOfSize:14] context:nil lineSpaceing:8];

            NSString *secondAddressStr = [NSString stringWithFormat:@"%@",[YSBCommonTool isBlankString:address.formatted_address] ? @"" : address.formatted_address];
            CGSize secondAddressSize = [NSString sizeWithText:secondAddressStr maxContentSize:CGSizeMake(addressWidth, CGFLOAT_MAX) font:[UIFont systemFontOfSize:12] context:nil lineSpaceing:6];

            NSString *thirdAddressStr = [NSString stringWithFormat:@"%@",[YSBCommonTool isBlankString:address.address] ? @"" : address.address];
            CGSize thirdAddressSize = [NSString sizeWithText:thirdAddressStr maxContentSize:CGSizeMake(addressWidth, CGFLOAT_MAX) font:[UIFont systemFontOfSize:14] context:nil lineSpaceing:8];

            return 270 + firstAddressSize.height + secondAddressSize.height + thirdAddressSize.height;
            
        }else if ((indexPath.section == 6 && !self.orderDetailModel.infoModel.insurance)
                  || (indexPath.section == 7 && self.orderDetailModel.infoModel.insurance)) {
            //订单信息
            if (self.orderStatus == 2) {//进行中的订单
                return 20 + 20 + 20 + 15 + (15 + 15) + 20;
            }
            return 20 + 20 + 20 + 15 + (15 + 15) + (15 + 15) + 20;//结束和取消订单
            
        }else if (self.orderStatus == 2
                  && ((indexPath.section == 7 && !self.orderDetailModel.infoModel.insurance) || (indexPath.section == 8 && self.orderDetailModel.infoModel.insurance))) {
            //取消订单
            return 49;
        }
    }
    return 0;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    BOOL insurance = self.orderDetailModel.infoModel.insurance;
    if ((section == 7 && !insurance) || (section == 8 && insurance)) {
        return 20;
    }
    else if (section == 1) {
        return 0.1;
    }
    return 10;
}


@end



@implementation YSBOfflineOrderInfoAnalysisCell

- (void)setModel:(YSBOfflineAnalysisModel *)model {
    _model = model;
 
    self.pushNumLabel.text = [NSString stringWithFormat:@"%ld",model.push_nums];
    self.abandonLabel.text = [NSString stringWithFormat:@"%ld",model.abandon_nums];
    self.grabbedLabel.text = [NSString stringWithFormat:@"%ld",model.take_nums];
    
    
    YSBOfflineReasonModel *reason = model.reason;
    NSArray *reasonArray = @[@"工价太低",@"标题不清晰",@"考虑中",@"放弃接单"];
    for (NSInteger i = 0; i < reasonArray.count; i++) {
        NSString *numStr = @"";

        switch (i) {
            case 0:{
                if (reason.less_money >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.less_money];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.less_money];
                }
                self.priceReasonLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            case 1:{
                if (reason.unclear >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.unclear];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.unclear];
                }
                self.titleReasonLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            case 2:{
                if (reason.collect >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.collect];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.collect];
                }
                self.consideringLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            case 3:{
                if (reason.abandon >= 10) {
                    numStr = [NSString stringWithFormat:@"%ld+",reason.abandon];
                }
                else{
                    numStr = [NSString stringWithFormat:@"%ld",reason.abandon];
                }
                self.giveUpLabel.text = [NSString stringWithFormat:@"%@ (%@)",reasonArray[i],numStr];
            }
                break;
            default:
                break;
        }
    }
    
    [self.contentView removeAllSubviews];
    self.contentView.hidden = true;
    
}


@end


@implementation YSBOfflineOrderInfoViewController

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 20;
    }
    else if (section == 1) {
        return 0.1;
    }
    return 10;
}

@end
